import AMapLoader from '@amap/amap-jsapi-loader'
function ts2str(ts) {
    let date = new Date(ts * 1000)
    let year = date.getFullYear()
    let month = date.getMonth()
    month = month < 9 ? `0${month+1}` : month+1
    let day = date.getDate()
    day = day < 10 ? `0${day}` : day
    let hour = date.getHours()
    hour = hour < 10 ? `0${hour}` : hour
    let minute = date.getMinutes()
    minute = minute < 10 ? `0${minute}` : minute
    let second = date.getSeconds()
    second = second < 10 ? `0${second}` : second
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}

function pos2str(lon,lat){
    AMapLoader.load({
        "key": "ef777f1f8f0b6cd4ac27d6b2153d9693",   // 申请好的Web端开发者Key，首次调用 load 时必填
        "version": "2.0",   // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        "plugins": []  //插件列表
    }).then((AMap)=>{
        let map = new AMap.Map('ctn', {
            center: [116.397428, 39.90923],
            zoom: 13
        });
        map.plugin('AMap.Geocoder', function() {
            let geocoder = new AMap.Geocoder({
                radius:1000,
                extensions:'all',
                city:'010'
            })
            geocoder.getAddress([lon,lat],function(status,result){
                if(status=='complete')
                {
                    console.log(result)
                }
                else{
                    console.log('sb')
                }
            })
        })
    })
}

export default (context, inject) => {
    let repositories = {
        ts2str: ts2str,
        pos2str: pos2str
    }

    inject('tool', repositories)
}